package com.iotechn.unimall.app.api.examineAndApprove;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobbinsoft.fw.core.Const;
import com.dobbinsoft.fw.core.exception.AppServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.core.util.SessionUtil;
import com.iotechn.unimall.data.domain.ExamineAndApproveDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.dto.AdminDTO;
import com.iotechn.unimall.data.dto.UserDTO;
import com.iotechn.unimall.data.enums.ExamineAndApproveType;
import com.iotechn.unimall.data.enums.UserStatusType;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ExamineAndApproveMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service("APPExamineAndApprove")
public class ExamineAndApproveServiceImpl extends ServiceImpl<ExamineAndApproveMapper, ExamineAndApproveDO> implements ExamineAndApproveService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate userRedisTemplate;

    @Autowired
    private SessionUtil<UserDTO, AdminDTO> sessionUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(Long associationId,Long enterpriseId, Integer type,String accessToken, Long userId) throws ServiceException {
        ExamineAndApproveDO examineAndApproveDO = new ExamineAndApproveDO();
        examineAndApproveDO.setAssociationId(associationId);
        examineAndApproveDO.setUserId(userId);
        examineAndApproveDO.setEnterpriseId(enterpriseId);
        examineAndApproveDO.setStatus(ExamineAndApproveType.IN_THE_APPLICATION.getCode());
        examineAndApproveDO.setGmtCreate(new Date());
        examineAndApproveDO.setType(type);
        if(baseMapper.insert(examineAndApproveDO) <= 0){
            throw new AppServiceException(ExceptionDefinition.APP_UNKNOWN_EXCEPTION);
        }

        //修改状态
        QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserDO::getId,userId);
        UserDO userDO = userMapper.selectOne(wrapper);

        //更新SESSION缓存
        UserDTO user = sessionUtil.getUser();

        if(ExamineAndApproveType.TYPE_JOIN.getCode().equals(type)){
            userDO.setStatus(UserStatusType.JOIN_AUDIT.getCode());
            user.setStatus(UserStatusType.JOIN_AUDIT.getCode());
        }else if(ExamineAndApproveType.TYPE_CANCEL.getCode().equals(type)){
            userDO.setStatus(UserStatusType.CANCEL_AUDIT.getCode());
            user.setStatus(UserStatusType.CANCEL_AUDIT.getCode());
        }

        userRedisTemplate.opsForValue().set(Const.USER_REDIS_PREFIX + accessToken, JSONObject.toJSONString(user));

        if(userMapper.updateById(userDO) <= 0){
            throw new AppServiceException(ExceptionDefinition.APP_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

}
